package datastructure_algorithm.sort;

import java.lang.reflect.Method;

public class SortTestHelper {

	// 生成有n个元素的随机数组，每个元素的随机范围为[rangeL,rangeR]
	public static Integer[] generateRandomArray(int n, int rangeL, int rangeR) {
		assert rangeL <= rangeR;

		Integer[] arr = new Integer[n];
		for (int i = 0; i < n; i++) {
			arr[i] = (int) (Math.random() * (rangeR - rangeL + 1) + rangeL);
		}
		return arr;
	}

	public static void printArray(Integer[] arr) {
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i]);
			System.out.print(" ");
		}
		System.out.println();
	}

	public static boolean isSort(Integer[] arr) {
		for (int i = 0; i < arr.length; i++) {
			if (arr[i] > arr[i + 1]) {
				return false;
			}
		}
		return true;
	}

	public static void testSort(String className, Integer[] arr) {
		try {
			Class<?> clazz = Class.forName(className);
			Method method = clazz.getMethod("sort", Integer[].class);
			Object[] param = new Object[] {arr};
			long start = System.currentTimeMillis();
			method.invoke(null, param);
			long end = System.currentTimeMillis();
			assert isSort(arr);
			System.out.println(clazz.getSimpleName() + " : " + (end - start) + "ms");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Integer[] arr = generateRandomArray(5, 5, 10);
		printArray(arr);
		System.out.println(isSort(arr));
	}

}
